package week.four;

import java.util.*;

import android.util.Log;

public class EntityManager {

	private HashMap<String, Entity> entities = new HashMap<String, Entity>();
	private HashMap<String, ArrayList<Entity>> groups = new HashMap<String, ArrayList<Entity>>();
	
	public EntityManager() {
		
	}
	
	public void update(float dt) {
		
	}
	
	public void onDraw() {
		
	}
	
	public void addEntity(Entity e) {
		// Add entity to entities if an entity with the same name does not exist.
		if (!entities.containsKey(e.name)) {
			entities.put(e.name, e);
			Log.d("EntityManager::addEntity", "Entity with name "+e.name+" added to entity list");
			// Add entity to group if it has one.
			if (!groups.containsKey(e.group) && e.group != null) { 
				// Create new list.
				ArrayList<Entity> list = new ArrayList<Entity>();
				list.add(e);
				groups.put(e.group, list);
				Log.d("EntityManager::addEntity","Entity with name "+e.name+" added to new group "+e.group);
			}
			else {
				// Add to existing list.
				groups.get(e.group).add(e);
				Log.d("EntityManager::addEntity","Entity with name "+e.name+" added to group "+e.group);
			}
		}
		else
			Log.w("EntityManager::addEntity","Entity with name "+e.name+" already exists.");
	}
	
	public void removeEntity(String name) {
		if (entities.containsKey(name)) {
			Log.d("EntityManager::removeEntity","Removed Entity with name "+name+".");
			entities.remove(name);
		}
		else
			Log.w("EntityManager::removeEntity", "Entity with name "+name+" does not exist.");
	}
	
	public Entity getEntity(String name) {
		if (entities.containsKey(name)) {
			Log.d("EntityManager::getEntity","Returned Entity with name "+name+".");
			return entities.get(name);
		}
		else {
			Log.w("EntityManager::getEntity", "Entity with name "+name+" does not exist.");
			return null;
		}
	}
	
	public ArrayList<Entity> getGroup(String group) {
		if (group != "All") {
			if (entities.containsKey(group)) {
				Log.d("EntityManager::getGroup","Returned Entity Group "+group+".");
				return groups.get(group);
			}
			else {
				Log.w("EntityManager::getGroup", "Entit Group "+group+" does not exist.");
				return null;
			}
		}
		else {
			if (entities.values() != null) {
				ArrayList<Entity> list = (ArrayList<Entity>)entities.values();
				Log.d("EntityManager::getGroup", "Returned entire Entity Group.");
				return list;
			}
			else {
				Log.w("EntityManager::getGroup", "No entites exist.");
				return null;
			}
		}
	}
}
